import re
from django.utils.text import wrap
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.sites.models import Site
from django.template import Context, loader
from django.template.loader import render_to_string
from django.conf import settings

# favour django-mailer but fall back to django.core.mail

#if "mailer" in settings.INSTALLED_APPS:
#    from mailer import send_mail
#else:
from django.core.mail import send_mail
    
def format_quote(sender, body):
    """
    Wrap text at 55 chars and prepends each
    line with '> '.
    Used for quoting messages in replies.
    """    
    lines = wrap(body, 55).split('|n')
    for i, line in enumerate(lines):
        lines[i] = "> %s" % line
    quote = '\n'.join(lines)
    return ugettext(u"%(sender)s wrote:\n%(body)s") % {
       'sender': sender,
       'body': quote
    }
    
def format_subject(subject):
    """
    Prepends 'Re:' to the subject. To avoid multiple 'Re:'s
    a counter is added.
    NOTE: Currently unused. First step to fix Issue #48: 
    http://code.google.com/p/django-messages/issues/detail?id=48.
    FIXME: Any hints how to make this i18n aware are very welcome.
    """
    subject_prefix_re = r'^Re\[(\d*)\]:\ '
    m = re.match(subject_prefix_re, subject, re.U)
    prefix = u""
    if subject.startswith('Re: '):
        prefix = u"[2]"
        subject = subject[4:]
    elif m is not None:
        try:
            num = int(m.group(1))
            prefix = u"[%d]" % (num + 1)
            subject = subject[6 + len(str(num)):]
        except:
            # if anything fails here, fall back to the old mechanism
            pass
        
    return ugettext(u"Re%(prefix)s: %(subject)s") % {
        'subject': subject,
        'prefix': prefix
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
